package com.cloudspace.jindun.manager;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.dao.PhoneRecord;
import com.cloudspace.jindun.dao.PhoneRecordDao;
import com.cloudspace.jindun.utils.ObjUtil;

import java.util.List;

/**
 * Created by zengxianhua on 16/12/22.
 */

public class PhoneRecordManager {
    private static PhoneRecordManager instance;

    public synchronized static PhoneRecordManager getInstance(){
        if (instance == null){
            instance = new PhoneRecordManager();
        }

        return instance;
    }

    public  void addPhoneRecord(PhoneRecord record){
        if (JindunApp.getInstance().getDaoSession() == null) {
            return;
        }

        JindunApp.getInstance().getDaoSession().getPhoneRecordDao().insert(record);
    }

    public  void deletePhoneRecord(String userId){
        if (JindunApp.getInstance().getDaoSession() == null) {
            return;
        }

        List<PhoneRecord> result = JindunApp.getInstance().getDaoSession().getPhoneRecordDao().queryBuilder().where(PhoneRecordDao.Properties.Userid.eq(userId)).list();

        if (ObjUtil.isEmpty(result)){
            return;
        }

        for (PhoneRecord record : result){
            JindunApp.getInstance().getDaoSession().getPhoneRecordDao().delete(record);
        }
    }

    public List<PhoneRecord> queryAllRecords(){
        if (JindunApp.getInstance().getDaoSession() == null) {
            return null;
        }

        return JindunApp.getInstance().getDaoSession().getPhoneRecordDao().queryBuilder().list();
    }
}
